//
//  YsqhV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/20.
//

import Foundation

protocol YsqhVDelegate: class {
    func selectIndex(view: YsqhV!, name: String, index: [Int])
}

class YsqhV: UIView {
    
    weak var delegate: YsqhVDelegate?
    
    var bgView: UIView!
    
    var label: UILabel!
    
    var datas: [String]!
    
    var picker: UIPickerView!
    
    var selectIndex: [Int]!
    
    var callBack: ((YsqhV?, String, [Int]) -> Void)!
    
    var collectionView: UICollectionView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    func showIn(_ SV: UIView, _ top: CGFloat, _ datas: [String], _ selectIndex: [Int], _ callBack: @escaping (YsqhV?, String, [Int]) -> Void) -> Void {
        if datas.count == 0 {
            return
        }
        self.datas = datas
        self.clipsToBounds = true
        self.selectIndex = selectIndex
        self.callBack = callBack
        SV.addSubview(self)
        self.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(top)
            make.left.bottom.right.equalToSuperview()
        }
        
        config()
        
        bgView.transform = CGAffineTransform.init(translationX: 0, y: -min(160, (CGFloat(ceil(CGFloat(self.datas.count) / 4.0) * 50 + 60))))
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.35) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() -> Void {
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(min(160, (CGFloat(ceil(CGFloat(self.datas.count) / 4.0) * 50 + 60))))
        }
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.lgf_Layout(0, 0, 0, 0, 0, 0))
        collectionView.backgroundColor = UIColor.white
        collectionView.lgf_Register(cellType: YsqhCell.self)
        collectionView.delegate = self
        collectionView.dataSource = self
        bgView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(UIDevice.lgf_IsIphoneX() ? 45 : 5)
            make.right.equalToSuperview().offset(-5)
            make.bottom.equalTo(-60)
        }
        
        let bottomView = UIView.init()
        bgView.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(60)
        }
        
        let qrButton = UIButton.init(type: .custom)
        qrButton.backgroundColor = UIColor.init(lgf_HexString: EWEDarkMode.c_0F88F2)!
        qrButton.setTitle("确定", for: .normal)
        qrButton.setTitleColor(EWEDarkMode.white(), for: .normal)
        qrButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        qrButton.layer.cornerRadius = 4
        qrButton.layer.masksToBounds = true
        qrButton.addTarget(self, action: #selector(qrButtonSelect(_:)), for: .touchUpInside)
        bottomView.addSubview(qrButton)
        qrButton.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(40)
        }
        
    }
    
    @objc func qrButtonSelect(_ sendder: UIButton) {
        self.delegate?.selectIndex(view: self, name:self.lgf_ViewName ?? "", index: self.selectIndex)
        self.callBack(self, self.lgf_ViewName ?? "", self.selectIndex)
        remove()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.35, animations: {
            self.bgView.transform = CGAffineTransform.init(translationX: 0, y: -160)
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension YsqhV : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: (UIDevice.lgf_ScreenH - (UIDevice.lgf_IsIphoneX() ? 50 : 10)) / 5, height: 50)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: YsqhCell.self, for: indexPath)
        cell.name.text = datas[indexPath.item]
        if self.selectIndex.contains(indexPath.item) {
            cell.name.backgroundColor = UIColor.init(lgf_HexString: "E1F1FF")
            cell.name.textColor = UIColor.init(lgf_HexString: "0F88F2")
        } else {
            cell.name.backgroundColor = UIColor.init(lgf_HexString: "F4F4F4")
            cell.name.textColor = UIColor.init(lgf_HexString: "8D969D")
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.selectIndex.contains(indexPath.item) {
            self.selectIndex.lgf_Remove(indexPath.item)
        } else {
            self.selectIndex.append(indexPath.item)
        }
        collectionView.reloadData()
    }
}

